package org.keshe.controllers;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import org.keshe.CurrentUser;
import org.keshe.data.StuData;
import org.keshe.data.StuDataWithButton;
import org.keshe.user.NormalUser;

import java.net.URL;
import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;

public class ThisBuildingStu implements Initializable {

    private NormalUser normalUser;
    @FXML
    private TableView<StuDataWithButton> table;

    @FXML
    private TableColumn<StuDataWithButton, String> s_id;

    @FXML
    private TableColumn<StuDataWithButton, String> sname;

    @FXML
    private TableColumn<StuDataWithButton, String> sex;

    @FXML
    private TableColumn<StuDataWithButton, String> major;

    @FXML
    private TableColumn<StuDataWithButton, String> bedName;

    public void loadTableContent(List<StuData> list){
        ObservableList<StuDataWithButton> observableList = FXCollections.observableArrayList();
        for(StuData stuData : list){
            observableList.add(new StuDataWithButton(stuData));
        }
        table.setItems(observableList);
    }
    @FXML

    void refreshClick(ActionEvent event) {
        try {
            List<StuData> thisBuildingStuData = normalUser.getThisBuildingStuData();
            loadTableContent(thisBuildingStuData);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        if(CurrentUser.USER instanceof NormalUser){
            normalUser = (NormalUser) CurrentUser.USER;
        } else{
            throw new RuntimeException();
        }

        s_id.setCellValueFactory(new PropertyValueFactory<>("s_id"));
        sname.setCellValueFactory(new PropertyValueFactory<>("sname"));
        sex.setCellValueFactory(new PropertyValueFactory<>("sex"));
        major.setCellValueFactory(new PropertyValueFactory<>("major"));
        bedName.setCellValueFactory(new PropertyValueFactory<>("bedName"));

    }
}
